// Written by Craig'n'Dave
using System;
// Binary search using an array/list
namespace ConsoleApp1
{
    class Program
    {
        static void binary_search(string[] items, string item_to_find)
        {
            bool found = false;
            int first = 0;
            int midpoint = 0;
            int last = items.Length - 1;
            // Repeat until the item is found or no items are left to check
            while (first <= last && found == false)
            {
                // Calculate the mid point using integer division
                midpoint = Convert.ToInt32((first + last) / 2);
                // Item found
                if (items[midpoint] == item_to_find)
                {
                    found = true;
                }
                else
                {
                    // Recalculate the mid point
                    // Using ordinal string comparison, returns < 0, 0 or > 0
                    if (String.Compare(items[midpoint], item_to_find) < 0)
                    {
                        first = midpoint + 1;
                    }
                    else
                    {
                        last = midpoint - 1;
                    }
                }
            }
            if (found == true)
            {
                Console.WriteLine("Item found at position " + midpoint);
            }
            else
            {
                Console.WriteLine("Item not found");
            }
        }

        // Main program starts here
        static void Main(string[] args)
        {
            string[] items = { "Alabama", "California", "Delaware", "Florida", "Georgia" };
            Console.WriteLine("Enter the state to find: ");
            string item_to_find = Console.ReadLine();
            binary_search(items, item_to_find);
        }
    }
}
